package com.zh.junit.controller;

import com.zh.junit.dao.Book;
import com.zh.junit.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author ZH
 * @date 12:18 2023/2/28
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/add")
    public Map addBook(@RequestBody Book book) {
        Long bookId = bookService.saveBook(book);
        return Map.of("bookId",bookId);
    }

    @PutMapping("/update/{id}")
    public Object editBook(@PathVariable Long id, @RequestBody Book book) {
        Long bookId = bookService.saveBook(book);
        return Map.of("bookId",bookId);
    }

    @DeleteMapping("/remove/{id}")
    public Object removeBook(@RequestBody Long id) {
        bookService.removeBook(id);
        return Map.of("removed",id);
    }

    @GetMapping("/search")
    public Object searchBook(@RequestParam String kw) {
        List<Book> books = bookService.searchByTitle(kw);
        return Map.of("list",books);
    }

    @GetMapping("/list")
    public Object findAll() {
        List<Book> books = bookService.findAll();
        return Map.of("list",books);
    }

}
